<?php namespace app\common\model;
use think\Loader;
use think\Validate;
use think\Db;

//用户表模型
class Users extends Base{
//	  protected $insert = ['register_time'=>time()];  
//	  protected function setRegisterTimeAttr()
//		{
//		    return time();
//		}
	//添加管理员数据
	public function addData($data){
		
		$validate = Loader::validate('Users');
		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}else{
			$data['password']=md5($data['password']);
				 $users = new Users();
         $users->allowField(true)->save($data);
////			var_dump($data);
//		
//			Db::name('users')->insert($data);
////			$user->allowField(true)->save();
          //获取自增主键
          $id=$users->id;
          
          if($id){
          	 return ['valid'=>1,'msg'=>'管理员添加成功！','id'=>$id];
          }else{
          	 return ['valid'=>0,'msg'=>'管理员添加失败！'];
          }
			
		}
		
	}
	
	
	//添加管理员数据
	public function editData($data){
		
		$validate = Loader::validate('Users');
		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}else{
			  $data['password']=md5($data['password']);
			  //更新users表的数据
		      $users = new Users();
              $num=$users->allowField(true)->save($data,['id' =>$data['id']]);
////			var_dump($data);
//		
//			Db::name('users')->insert($data);
////			$user->allowField(true)->save();
//          unset($data['comfirmpassword']);
//          $num=Db::table('t_users')
//                  ->where('id',$data['id'])
//                  ->update($data);
//        var_dump($num);
//        halt($data['id']);
          if($num){
          	 return ['valid'=>1,'msg'=>'用户表修改成功！','id'=>$data['id']];
          }else{
          	 return ['valid'=>0,'msg'=>'用户表修改失败！'];
          }
			
		}
		
	}
	
	
	
}


